package hu.test.atacama;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class GetLocation extends Service implements LocationListener {

	public static final String LOCATION_CHANGED = "hu.test.atacama.LOCATION_CHANGED";
	private final IBinder mBinder = new LocalBinder();
	protected LocationManager lm;
	protected Location userLocation = null;
	Common common;

	@Override
	public void onCreate() {
		super.onCreate();
		common = Common.getInstance(this);
		// common.displayToast("getlocation oncreate");
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		/*
		 * Criteria criteria = new Criteria();
		 * criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		 * criteria.setAltitudeRequired(false);
		 * criteria.setBearingRequired(false); criteria.setCostAllowed(true);
		 * String provider = lm.getBestProvider(criteria, true);
		 */
		lm.requestLocationUpdates(common.getCurrentProvider(), 0, 0, this);
	}

	@Override
	public void onDestroy() {
		// common.displayToast("getlocation ondestroy");
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// common.displayToast("onstartcommand eleje");

		// super.onStartCommand(intent, flags, startId);
		// return START_STICKY;
		return super.onStartCommand(intent, flags, startId);
	}

	public class LocalBinder extends Binder {
		GetLocation getService() {
			return GetLocation.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public void onLocationChanged(Location location) {
		Intent broadcast = new Intent(LOCATION_CHANGED);
		broadcast.setPackage(getPackageName());
		GetLocation.this.sendBroadcast(broadcast);
		try {
			if (location != null) {
				common.storeGPSPosition(location);
				userLocation = location;
			}
		} catch (Exception ex) {
			common.displayToast("GetGPSData / onLocationChanged: "
					+ ex.toString());
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(getApplicationContext(),
				R.string.message_gpsStateChangedToOff, Toast.LENGTH_LONG)
				.show();
		lm.removeUpdates(this);
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	public Double getLatitude() {
		if (userLocation != null) {
			return userLocation.getLatitude();
		} else {
			return (double) 0;
		}
	}

	public Double getLongitude() {
		if (userLocation != null) {
			return userLocation.getLongitude();
		} else {
			return (double) 0;
		}
	}

	public Long getTime() {
		if (userLocation != null) {
			return userLocation.getTime();
		} else {
			return (long) 0;
		}
	}

	public Float getAccuracy() {
		if (userLocation != null) {
			return userLocation.getAccuracy();
		} else {
			return (float) 0;
		}
	}

	public Double getAltitude() {
		if (userLocation != null) {
			return userLocation.getAltitude();
		} else {
			return (double) 0;
		}
	}

	public Float getSpeed() {
		if (userLocation != null) {
			return userLocation.getSpeed();
		} else {
			return (float) 0;
		}
	}

	public String getProvider() {
		if (userLocation != null) {
			return userLocation.getProvider();
		} else {
			return "No data";
		}
	}
}
